//
//  ZSHotViewController.m
//  ZSMiao
//
//  Created by zsj1992 on 16/12/1.
//  Copyright © 2016年 bjhj. All rights reserved.
//

#import "ZSHotViewController.h"
#import "HYBNetworking.h"
#import "ZSTopAd.h"
#import <SDCycleScrollView.h>
#import "CommonWebviewController.h"
#import "ZSLive.h"
#import "ZSHotCell.h"
#import "ZSPlayViewController.h"
#import "ZSLiveCollectionViewController.h"
@interface ZSHotViewController ()<UITableViewDelegate,UITableViewDataSource,SDCycleScrollViewDelegate>

@property (strong, nonatomic) UITableView *tableView;

@property (nonatomic,strong)NSMutableArray * ads;
@property (nonatomic,strong)NSMutableArray * lives;



@end

@implementation ZSHotViewController

-(NSMutableArray *)ads{
    if (_ads==nil) {
        _ads = [NSMutableArray array];
    }
    return _ads;
}

-(NSMutableArray *)lives{

    if (_lives==nil) {
        _lives = [NSMutableArray array];
    }
    return _lives;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_H-104)];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
    
    
    
    [self loadAD];

    

    
}

-(void)btnClick{

    MyLog(@"点击了");
    UIViewController * vc = [[UIViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}


-(void)loadAD{
    [HYBNetworking getWithUrl:@"http://live.9158.com/Living/GetAD" refreshCache:NO params:nil success:^(id response) {
        
        NSDictionary * responseDic = (NSDictionary *)response;
        
        NSArray * dataArr = responseDic[@"data"];
        
        NSMutableArray * arrTemp = [NSMutableArray array];
        
        for (NSDictionary * dic in dataArr) {
            ZSTopAd * topAd = [ZSTopAd mj_objectWithKeyValues:dic];
            [arrTemp addObject:topAd];
        }
        self.ads = arrTemp;
        
        [self getHotLiveList];
        
    } fail:^(NSError *error) {
    
    
    }];
}


NSInteger page;

- (void)getHotLiveList
{
    
    page=1;
    
    NSString * urlStr = [NSString stringWithFormat:@"http://live.9158.com/Fans/GetHotLive?page=%ld", page];

    [HYBNetworking getWithUrl:urlStr refreshCache:NO params:nil success:^(id response) {
        
        NSDictionary * responseDic = (NSDictionary *)response;
        
        NSArray * resultArr = responseDic[@"data"][@"list"];

        NSMutableArray * arrTemp = [NSMutableArray array];
        
        for (NSDictionary * dic in resultArr) {
            
            ZSLive * live = [ZSLive mj_objectWithKeyValues:dic];
            
            [arrTemp addObject:live];
        }
        
        self.lives = arrTemp;
        
        [self.tableView reloadData];
        
    } fail:^(NSError *error) {
        
        
    }];
    
    
    

    
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.lives.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ZSLive * live = self.lives[indexPath.row];
    
    ZSHotCell * cell = [ZSHotCell cellWithTableView:tableView];
    
    cell.live = live;
    
    return cell;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    UIView * header = [[UIView alloc]init];
    header.backgroundColor = [UIColor groupTableViewBackgroundColor];    SDCycleScrollView * adScrollView = [[SDCycleScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_W, 100)];
    
    adScrollView.delegate = self;
    
    NSMutableArray * imageURLGroups = [NSMutableArray array];
    
    for (ZSTopAd * topAd in self.ads) {
        
        [imageURLGroups addObject:topAd.imageUrl];
    }
    
    adScrollView.imageURLStringsGroup = imageURLGroups;
    
//    [header addSubview:adScrollView];
    
    return header;
    
}


-(void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index{

    ZSTopAd * topAd  = self.ads[index];
    
    CommonWebviewController * webVc = [[CommonWebviewController alloc]init];
    
    webVc.title = topAd.title;
    
    NSURL * url = [NSURL URLWithString:topAd.link];
    
    webVc.URL = url;
    
    [self.navigationController pushViewController:webVc animated:YES];
    
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 469;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return 100;
}



//选中某个按钮
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ZSLiveCollectionViewController * liveCollectionVc = [[ZSLiveCollectionViewController alloc]init];
    
    liveCollectionVc.lives = self.lives;
    
    liveCollectionVc.currentIdx = indexPath.row;
    
    [self.navigationController pushViewController:liveCollectionVc animated:YES];
    
    
    

}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}


@end
